//package com.cloudpivot.project.rabbitmq.service;
//
//import com.rabbitmq.client.*;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import java.io.IOException;
//import java.util.concurrent.TimeoutException;
//
//@Component
//public class ConsumerService implements CommandLineRunner {
//    @Value("${rabbitmq-config.host-name}")
//    private String HOST_NAME;
//    @Value("${rabbitmq-config.host-port}")
//    private Integer HOST_PORT;
//    @Value("${rabbitmq-config.username}")
//    private String USER_NAME;
//    @Value("${rabbitmq-config.password}")
//    private String PASSWORD;
//    private static final String QUEUE_NAME = "webTest1";
//    private static final String EXCHANGE_NAME = "amq-direct";
//    private static final String VIRTUAL_HOST = "/mirror";
//
//    @Override
//    public void run(String... args) throws Exception {
//        firstCumsumer();
//    }
//
//    private void firstCumsumer() {
//        try {
//            System.out.println("等待方法");
//            ConnectionFactory factory=new ConnectionFactory();
//            factory.setHost(HOST_NAME);
//            factory.setPort(HOST_PORT);
//            factory.setUsername(USER_NAME);
//            factory.setPassword(PASSWORD);
//            factory.setVirtualHost(VIRTUAL_HOST);
//            Connection rabbitMQConnection= factory.newConnection();
//            Channel channel = rabbitMQConnection.createChannel();
//            channel.queueDeclare(QUEUE_NAME,true,false,false,null);
//            channel.basicQos(1);
//            Consumer myConsumer=new DefaultConsumer(channel){
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    String routingKey = envelope.getRoutingKey();
//                    String contentType = properties.getContentType();
//                    String deliveryTag = String.valueOf(envelope.getDeliveryTag());
//                    String message=new String(body,"UTF-8");
//                    System.out.println("=======================");
//                    System.out.println("获得的消息为=====>>" + message);
//                    System.out.println("该消息对应的参数routingKey:"+routingKey+" deliveryTag:"+deliveryTag+" contentType:"+contentType);
//                    channel.basicAck(envelope.getDeliveryTag(),false);
//                }
//            };
//            channel.basicConsume(QUEUE_NAME, false, myConsumer);
//            System.out.println("打印rabbitmq 接收到的内容");
//        } catch (IOException e) {
//            System.out.println("IO异常");
//            throw new RuntimeException(e);
//        } catch (TimeoutException e) {
//            System.out.println("等待超时");
//            throw new RuntimeException(e);
//        }
//
//    }
//}
